<!DOCTYPE html>
<html lang="en">
<title>添加新文章</title>
<#import "/layout/layout.html" as body/>
<@body.page>

<!--  <div class="row wrapper border-bottom white-bg page-heading">
     <div class="col-lg-10">
         <h2>运营位管理</h2>
         <ol class="breadcrumb">
             <li><a href="index.html">运营位管理</a></li>
             <li><a href="">文章</a></li>
             <li class="active"><strong>添加新文章</strong></li>
         </ol>
     </div>
 </div>
  -->
<div class="wrapper wrapper-content animated fadeInRight">
    <div class="banner-add-box col-lg-12">
        <div class="ibox float-e-margins">
            <div class="ibox-title">
                <h5>添加新文章</h5>
            </div>
            <div class="ibox-content">
                <form method="post" id="uploadForm" name="fileinfo" class="form-horizontal" enctype="multipart/form-data">
                    <div class="form-group"><label class="col-sm-2 control-label">文章标题</label>
                        <div class="col-sm-10">
                            <input type="text" id="title" name="title" class="form-control">
                            <span class="help-block m-b-none text-right">0/50</span>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-2 control-label">作者</label>
                        <div class="col-sm-10">
                            <select name="coachId" id="coachId" data-placeholder="请输入教练" class="form-control m-b" tabindex="2">
                                <option value="">请选择</option>
                                <#list coachInfos as item>
                                    <option value="${item.coachId}">${item.coachName}</option>
                                </#list>
                            </select>
                            <!--<input type="url" name="previewUrl" id="previewUrl" class="form-control">-->
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-2 control-label">文章链接</label>
                        <div class="col-sm-10">
                            <input type="text" name="link" id="link" class="form-control">
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-2 control-label">摘要</label>
                        <div class="col-sm-10">
                            <input type="text" name="remark" id="remark" class="form-control">
                            <span class="help-block m-b-none pull-right">0/50</span>
                        </div>
                    </div>
                    <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-2 control-label">封面图</label>
                        <div class="col-sm-10">
                            <label for="file" class="file-input">
                                <input type="file" name="file" id="file"/>
                                <svg width="20" height="20" viewBox="0 0 20 17">
                                    <path d="M10 0l-5.2 4.9h3.3v5.1h3.8v-5.1h3.3l-5.2-4.9zm9.3 11.5l-3.2-2.1h-2l3.4 2.6h-3.5c-.1 0-.2.1-.2.1l-.8 2.3h-6l-.8-2.2c-.1-.1-.1-.2-.2-.2h-3.6l3.4-2.6h-2l-3.2 2.1c-.4.3-.7 1-.6 1.5l.6 3.1c.1.5.7.9 1.2.9h16.3c.6 0 1.1-.4 1.3-.9l.6-3.1c.1-.5-.2-1.2-.7-1.5z"/>
                                </svg>
                                <span>选择文件 ...</span>
                            </label>
                        </div>
                    </div>
                    <!-- <div class="hr-line-dashed"></div>
                    <div class="form-group"><label class="col-sm-12">正文编辑器</label></div>
                    <div class="ibox float-e-margins" style="border: 1px solid #ccc">
                        <div class="ibox-content no-padding">
                            <div class="summernote">
                                输入内容
                                <h3>Lorem Ipsum is simply</h3>
                            </div>
                        </div>
                    </div> -->
                    <div class="hr-line-dashed"></div>
                    <div class="form-group">
                        <div class="col-sm-12">
                            <button type="button" class="btn btn-default pull-left">取消修改</button>
                            <button type="button" id="add_article" class="btn btn-success pull-right">确定添加</button>
                            <button type="button" style="display:none" class="btn btn-primary pull-right">内网预览</button>
                        </div>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>

<script src="${baseUrl}js/plugins/summernote/summernote.min.js"></script>
<script>
    $(document).ready(function () {

        $("#add_article").click(function () {
            add_article();
        });

        $('.summernote').summernote();

        var edit = function () {
            $('.click2edit').summernote({
                focus: true
            });
        };
        var save = function () {
            var aHTML = $('.click2edit').code(); //save HTML If you need(aHTML: array).
            $('.click2edit').destroy();
        };

    });

    function add_article() {
        var title = Trim($("#title").val(), 'g');
        var remark = Trim($("#remark").val(), 'g');
        var link = Trim($("#link").val(), 'g');
        if (title == '') {
            swal("标题不能为空!", '', 'error');
            $("#title").focus();
            return;
        }
        var coachId = $("#coachId option:selected").val();
        if (coachId == '') {
            swal("请选择作者!", '', 'error');
            $("#coachId").focus();
            return;
        }
        if (link == '') {
            swal("请输入文章链接!", '', 'error');
            $("#link").focus();
            return;
        }
        if (remark == '') {
            swal("请输入摘要!", '', 'error');
            $("#remark").focus();
            return;
        }
        var flag = false;

        $("#file").each(function () {
            if ($(this).val() == "") {
                swal("请选择封面图！", '', 'error');
                flag = true;
            }
        });

        if (flag == false) {
            var url = 'add_acricle';
            var formData = new FormData($("#uploadForm")[0]);

            swal({
                title: '确认添加',
                text: "",
                type: 'question',
                showCancelButton: true,
                cancelButtonColor: '#d33',
                confirmButtonText: 'Yes, 确认添加',
                cancelButtonText: 'No, 再次确认',
            }).then(function () {
                $.ajax({
                    url: url,
                    type: 'POST',
                    data: formData,
                    async: false,
                    cache: false,
                    contentType: false,
                    processData: false,
                    success: function (returndata) {
                        if (returndata > 0) {
                            swal("添加成功!", "页面即将跳转,请稍后...", "success");
                            window.setTimeout(function () {
                                window.location.href = 'to_operating_manage?1=1#tab-4';
                            }, 1000)
                        }
                    },
                    error: function (returndata) {
                        swal(returndata, '', 'error');
                    }
                });
            }, function () {
            });
        }
    }


    function Trim(str, is_global) {
        var result;
        result = str.replace(/(^\s+)|(\s+$)/g, "");
        if (is_global.toLowerCase() == "g")
            result = result.replace(/\s/g, "");
        return result;
    }
</script>

</@body.page>
</html>